/*
 *    Copyright (c) 2023-2025, chenda All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the cfp4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: chenda (chendainfo@gmail.com)
 */

package com.cfp4cloud.cfp.admin.handler;

import com.cfp4cloud.cfp.admin.api.dto.UserDTO;
import com.cfp4cloud.cfp.admin.api.dto.UserInfo;
import com.cfp4cloud.cfp.admin.api.entity.SysUser;
import com.cfp4cloud.cfp.admin.service.SysUserService;
import com.cfp4cloud.cfp.common.core.util.R;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author chenda
 * @date 2018/11/18
 */
@Slf4j
@Component("SMS")
@AllArgsConstructor
public class SmsLoginHandler extends AbstractLoginHandler {

	private final SysUserService sysUserService;

	/**
	 * 验证码登录传入为手机号 不用不处理
	 * @param mobile
	 * @return
	 */
	@Override
	public String identify(String mobile) {
		return mobile;
	}

	/**
	 * 通过手机号获取用户信息
	 * @param identify 手机号标识
	 * @return 用户信息，未找到时返回null
	 */
	@Override
	public UserInfo info(String identify) {
		UserDTO userDTO = new UserDTO();
		userDTO.setPhone(identify);

		R<UserInfo> userInfoR = sysUserService.getUserInfo(userDTO);

		if (userInfoR.getData() == null) {
			log.info("手机号 不存在用户:{}", identify);
			return null;
		}

		return userInfoR.getData();
	}

	/**
	 * 绑定逻辑
	 * @param user 用户实体
	 * @param identify 渠道返回唯一标识
	 * @return
	 */
	@Override
	public Boolean bind(SysUser user, String identify) {
		user.setPhone(identify);
		sysUserService.updateById(user);
		return true;
	}

}
